﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormProductInfo : Form
    {
        public FormProductInfo()
        {
            InitializeComponent();
        }

        private void FormProductInfo_Load(object sender, EventArgs e)
        {
            grid1.AutoRedraw = false;

            grid1.Cols = 27+4*Config.镀铜段数+1;

            grid1.DisplayFocusRect = false;
            grid1.DisplayRowArrow = true;
  
            grid1.BoldFixedCell = false;
            grid1.AllowUserPaste = FlexCell.ClipboardDataEnum.Text;
            grid1.DisplayRowNumber = true;

            grid1.Column(0).Width = 40;

            int col = 0;

            grid1.Cell(0, col++).Text = "序号";
            grid1.Cell(0, col++).Text = "ID";
            grid1.Cell(0, col++).Text = "料号";
            grid1.Cell(0, col++).Text = "宽度(inch)";
            grid1.Cell(0, col++).Text = "宽度(mm)";
            grid1.Cell(0, col++).Text = "上面电流密度ASD";
            grid1.Cell(0, col++).Text = "下面电流密度ASD";
            grid1.Cell(0, col++).Text = "镀铜时间";
            grid1.Cell(0, col++).Text = "镀铜速度";
            grid1.Cell(0, col++).Text = "上面镀铜厚度";
            grid1.Cell(0, col++).Text = "下面镀铜厚度";
            grid1.Cell(0, col++).Text = "板厚";
            grid1.Cell(0, col++).Text = "电镀效率";
            grid1.Cell(0, col++).Text = "卡板警报电压";

            grid1.Cell(0, col++).Text = "除油1频率";
            grid1.Cell(0, col++).Text = "除油2频率";
            grid1.Cell(0, col++).Text = "用户";
            grid1.Cell(0, col++).Text = "时间";
            grid1.Cell(0, col++).Text = "备注";

            for (int i = 1; i <= KorbeRTR.Config.镀铜段数; i++)
            {
                grid1.Cell(0, col++).Text = "铜" + i.ToString() + "电流系数";
                grid1.Cell(0, col++).Text = "铜" + i.ToString() + "上面电流";
                grid1.Cell(0, col++).Text = "铜" + i.ToString() + "下面电流";
                grid1.Cell(0, col++).Text = "铜" + i.ToString() + "频率";
            }

            grid1.AutoRedraw = true;
            grid1.Refresh();

            var list = RecipeView.ListAll();
            UpdateGrid(list);
            grid1.Cell(1, 1).SetFocus();
            
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <returns></returns>
        public void UpdateGrid(List<RecipeView> list)
        {     
            grid1.AutoRedraw = false;
            grid1.Rows = 1;
            grid1.Rows = list.Count + 10;

            foreach (var iter in list)
            {
                grid1.Cell(1, 1).Text = iter.Name;
            }

            for (int row = 1; row <= list.Count; row++)
            {
                var recipe = list[row - 1];

                int col = 1;

                grid1.Cell(row, col++).Text = recipe.Id.ToString();
                grid1.Cell(row, col++).Text = recipe.Name;
                grid1.Cell(row, col++).Text = recipe.WidthInch.ToString();
                grid1.Cell(row, col++).Text = recipe.Width.ToString();
                grid1.Cell(row, col++).Text = recipe.C面电流密度ASD.ToString();
                grid1.Cell(row, col++).Text = recipe.S面电流密度ASD.ToString();
                grid1.Cell(row, col++).Text = recipe.D镀铜时间.ToString();
                grid1.Cell(row, col++).Text = recipe.D镀铜速度mpmin.ToString();
                grid1.Cell(row, col++).Text = recipe.D镀铜C面厚度.ToString();
                grid1.Cell(row, col++).Text = recipe.D镀铜S面厚度.ToString();
                grid1.Cell(row, col++).Text = recipe.Thickness.ToString();
                grid1.Cell(row, col++).Text = recipe.D电流效率.ToString();
                grid1.Cell(row, col++).Text = recipe.K卡板报警电压.ToString();
                grid1.Cell(row, col++).Text = recipe.C除油1频率.ToString();
                grid1.Cell(row, col++).Text = recipe.C除油2频率.ToString();
                grid1.Cell(row, col++).Text = recipe.User;
                grid1.Cell(row, col++).Text = recipe.Time.ToString();
                grid1.Cell(row, col++).Text = recipe.Comment;

                for (int i = 0; i < KorbeRTR.Config.镀铜段数; i++)
                {
                    var slot = recipe.CuSlots[i];
                    grid1.Cell(row, col++).Text = slot.D电流系数.ToString();
                    grid1.Cell(row, col++).Text = slot.C面电流.ToString();
                    grid1.Cell(row, col++).Text = slot.S面电流.ToString();
                    grid1.Cell(row, col++).Text = slot.Flow.ToString();

                }

            }

            for(int i=1; i<grid1.Rows; i++)
            {
                for(int j=1; j<grid1.Cols; j++)
                {
                    grid1.Cell(i, j).Locked = true;
                }
            }

            grid1.AutoRedraw = true;
            grid1.Refresh();
        }

        /// <summary>
        /// 设置
        /// </summary>
        private void tsSet_Click(object sender, EventArgs e)
        {
            if (!Global.GetInstance().CurrentUser.Check生产料号操作权限())
            {
                MessageBox.Show("您没有生产料号操作权限, 请联系管理员!", "提示");
                return;
            }

            //先删除料号
            if (FormMainWindow.OmronPLC.SL上料参数.L料号ID.Value != 0)
            {
                MessageBox.Show("料号ID不为0，请先删除原来设定料号。", "提示");
                return;
            }

            var cell = grid1.Cell(grid1.Selection.FirstRow, 1);
            if (cell == null || string.IsNullOrWhiteSpace(cell.Text))
            {
                MessageBox.Show("未选择要设置的料号，请先选择料号!", "料号修改提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Int64 id = 0;
            try
            {
                id = Convert.ToInt64(cell.Text);
            }
            catch
            {
                MessageBox.Show("请选择料号!");
                return;
            }

            var gl = Global.GetInstance();

            gl.CurrentRecipeView = RecipeView.SelectById(id);
            if (gl.CurrentRecipeView != null)
                gl.FlagRefreshRecipe = true;

            Close();
        }

        //双击事件相当于点击设置
        private void grid1_DoubleClick(object Sender, EventArgs e)
        {
            tsChange_Click(Sender,e);
        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsAdd_Click(object sender, EventArgs e)
        {
            bool bl = CheckForm(typeof(FormAddProduct).Name);
            if (!bl)
            {
                FormAddProduct formAddProduct = new FormAddProduct(this);
                formAddProduct.ShowDialog();
                UpdateGrid(RecipeView.ListAll());
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsChange_Click(object sender, EventArgs e)
        {
            var cell = grid1.Cell(grid1.Selection.FirstRow , 1);
            if (cell == null || grid1.Selection.FirstRow ==0)
                return;

            if (cell == null || string.IsNullOrWhiteSpace(cell.Text))
            {
                MessageBox.Show("未选择要修改的料号，请先选择料号!", "料号修改提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else
            {
                //设置数据更新标识
                var gl = Global.GetInstance();

                long id = Convert.ToInt64(cell.Text);
                var recipe = RecipeView.SelectById(id);

                bool bl = CheckForm(typeof(FormAddProduct).Name);
                if (!bl)
                {
                    FormAddProduct formAddProduct = new FormAddProduct(this, recipe, true);
                    formAddProduct.ShowDialog();
                    UpdateGrid(FilterRecipe(TextBox_Filter.Text.Trim()));
                }
            }
                
           

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsDelete_Click(object sender, EventArgs e)
        {
            var gl = Global.GetInstance();

            if (!gl.CurrentUser.Check产品基本数据操作权限())
            {
                MessageBox.Show("没有产品基本数据操作权限! 请联系管理员.", "提示");
                return;
            }

            var cell = grid1.Cell(grid1.Selection.FirstRow, 1);
            var list = RecipeView.ListAll();

            if (grid1.Selection.FirstRow > list.Count + 1 || grid1.Selection.FirstRow == 0)
            {
                return;
            }
                

            if (cell == null || string.IsNullOrWhiteSpace(cell.Text))
            {
                MessageBox.Show("未选择要删除料号，请先选择料号!", "料号修改提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            
            else 
            {

                DialogResult result = MessageBox.Show("您是否要删除该料号？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    

                    var view = list[grid1.Selection.FirstRow - 1];

                    //添加记录
                    var record = new OperationRecord()
                    {
                        Time = DateTime.Now,
                        User = Global.GetInstance().CurrentUser.UserName,
                        Operation = "删除料号为:" + view.Name + "的产品基本资料",
                        OperationType = "新增/修改/删除/产品基本资料"
                    };

                    OperationRecord.Add(record);
                    RecipeView.Delete(view);
                    UpdateGrid(FilterRecipe(TextBox_Filter.Text.Trim()));
                }
                else
                {
                    
                }                                 
            }
            

                
        }

        private List<RecipeView> FilterRecipe(string filter)
        {
            if (string.IsNullOrWhiteSpace(filter))
            {
                return RecipeView.ListAll();
            }
            else
            {
                return RecipeView.FilterByName(filter);
            }
        } 

        public void UpdateByFilter()
        {
            UpdateGrid(FilterRecipe(TextBox_Filter.Text.Trim()));
        }

        private void TextBox_Filter_TextChanged(object sender, EventArgs e)
        {
            UpdateByFilter();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsRefresh_Click(object sender, EventArgs e)
        {
            UpdateGrid(RecipeView.ListAll());
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsHelp_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
